package com.myjava.managesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjava.managesystem.common.CustomException;
import com.myjava.managesystem.entity.Category;
import com.myjava.managesystem.entity.Dish;
import com.myjava.managesystem.entity.Setmeal;
import com.myjava.managesystem.mapper.CategoryMapper;
import com.myjava.managesystem.service.CategoryService;
import com.myjava.managesystem.service.DishService;
import com.myjava.managesystem.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService {

    @Autowired
    public DishService dishService;

    @Autowired
    public SetmealService setmealService;

    @Override
    public void removeCategory(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<Dish>();
        queryWrapper1.eq(Dish::getCategoryId,id);
        int count = (int) dishService.count(queryWrapper1);
        if(count>0){
            throw new CustomException("当前分类已关联菜品,无法删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<Setmeal>();
        queryWrapper2.eq(Setmeal::getCategoryId,id);
        count = (int) setmealService.count(queryWrapper2);
        if(count>0){
            throw new CustomException("当前分类已关联菜品,无法删除");
        }
        this.removeById(id);
    }
}
